package cn.wolfcode.wolf2world.service.impl;

import cn.wolfcode.wolf2world.domain.Destination;
import cn.wolfcode.wolf2world.domain.Strategy;
import cn.wolfcode.wolf2world.domain.StrategyCatalog;
import cn.wolfcode.wolf2world.domain.StrategyTheme;
import cn.wolfcode.wolf2world.query.StrategyQueryObject;
import cn.wolfcode.wolf2world.repository.StrategyRepository;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.StrategyCatalogService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.service.StrategyThemeService;
import cn.wolfcode.wolf2world.utils.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020/8/9 20:09
 */
@Service
public class StrategyServiceImpl implements StrategyService {
    
    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private StrategyCatalogService strategyCatalogService;
    @Autowired
    private StrategyThemeService strategyThemeService;
    @Autowired
    private DestinationService destinationService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(Strategy strategy) {
        strategy.setId(null);
        strategyRepository.insert(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return strategyRepository.findAll();
    }

    /**
     * 高级查询
     * @param qo
     * @return
     */
    @Override
    public Page<Strategy> query(StrategyQueryObject qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())) {
           query.addCriteria(Criteria.where("destId").is(qo.getDestId())) ;
        }
        if (StringUtils.hasLength(qo.getKeyword())) {
           query.addCriteria(Criteria.where("title").regex(qo.getKeyword())) ;
        }
        return DBHelper.getPage(mongoTemplate, query, qo, Strategy.class, Sort.Direction.ASC,"favornum");
    }


    /**
     * 新增或更新
     * @param strategy
     */
    @Override
    public void saveOrUpdate(Strategy strategy) {
        //维护前端没有提交过来的数据
        //"catalogName"
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setCatalogName(strategyCatalog.getName());
        //"destId":
        String destId = strategyCatalog.getDestId();
        strategy.setDestId(destId);
        //"destName":
        String destName = strategyCatalog.getDestName();
        strategy.setDestName(destName);
        //"themeName"
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //"isabroad"
        List<Destination> list = destinationService.toasts(destId);
        if (list != null && list.size() > 0) {
            Destination destination = list.get(0);
            strategy.setIsabroad("中国".equals(destination.getName()) ? Strategy.ABROAD_NO : Strategy.ABROAD_YES);
        }

        if (StringUtils.hasLength(strategy.getId())) {
            Strategy strategy1 = this.get(strategy.getId());
            //维护原来的数据
            //"viewnum":
            strategy.setViewnum(strategy1.getViewnum());
            //"replynum":
            strategy.setReplynum(strategy1.getReplynum());
            //"favornum":
            strategy.setFavornum(strategy1.getFavornum());
            //"sharenum":
            strategy.setSharenum(strategy1.getSharenum());
            //"thumbsupnum"
            strategy.setThumbsupnum(strategy1.getThumbsupnum());
            this.update(strategy);
        }else {
            //"createTime"
            strategy.setCreateTime(new Date());
            this.insert(strategy);
        }
    }

    /**
     * 根据攻略分类id查询攻略
     * @param id
     * @return
     */
    @Override
    public List<Strategy> queryByCatalogId(String id) {
        return strategyRepository.findByCatalogId(id);
    }


    /**
     * 根据目的地id查询攻略
     * @param destId
     * @return
     */
    @Override
    public List<Strategy> queryByDestId(String destId) {
        return strategyRepository.findByDestId(destId);
    }

    /**
     * 根据目的地id获取点击量前三的攻略
     * @param destId
     * @return
     */
    @Override
    public List<Strategy> strategiesViewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");
        return strategyRepository.findByDestId(destId,pageable);
    }

    /**
     * 根据目的地名称查询攻略
     * @param destName
     * @return
     */
    @Override
    public List<Strategy> queryByDestName(String destName) {
        return strategyRepository.findByDestName(destName);
    }


}
